//
//  MSPreferenceSetViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/1.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSPreferenceSetViewController.h"
#import "MSpreferenceSetTableVC.h"
#import "MSAreaViewController.h"
#import "MSindustryViewController.h"
#import "MSStageViewController.h"

@interface MSPreferenceSetViewController ()
@property (nonatomic, copy) NSString      *preferenceSetFile;
@property (nonatomic, strong) GXButton    *doneBtn;
@property (nonatomic, strong) NSMutableArray    *dataArray;     //数据
@property (nonatomic, strong) MSpreferenceSetTableVC *tableVC;
@end

@implementation MSPreferenceSetViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    if (_isFirst) {
        leftBarBtn.hidden = YES;
    }
    else{
        leftBarBtn.hidden = NO;
    }
    
    titleLable.text = @"项目偏好设置";
}

- (void)initUI{
    self.tableVC = [[MSpreferenceSetTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KNavBarHeight);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
    headerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableHeaderView = headerView;
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    footerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableFooterView = footerView;
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [footerView addSubview:horizonLineView];
    
    self.doneBtn = [[GXButton alloc] initWithFrame:CGRectMake(12, 25, SCREEN_WIDTH-24, 45)];
    self.doneBtn.titleLabel.font = HeitiFont(16);
    [self.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.doneBtn.backgroundColor = kBlueColor;
    self.doneBtn.layer.cornerRadius = 2;
    [self.doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:self.doneBtn];
}

- (void)testDataFunc{
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    self.preferenceSetFile = [documentsPath stringByAppendingPathComponent:@"preferenceSetFile.plist"];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:self.preferenceSetFile]) {
        NSMutableArray *resultArray = [[NSMutableArray alloc] initWithContentsOfFile:self.preferenceSetFile];
        _dataArray =  [NSMutableArray arrayWithArray:
                       @[[DOKeyValue key:@"地区" value:resultArray[0]],
                         [DOKeyValue key:@"行业" value:resultArray[1]],
                         [DOKeyValue key:@"项目阶段" value:resultArray[2]]
                         ]
                       ];
    }
    else{
        _dataArray =  [NSMutableArray arrayWithArray:
                       @[[DOKeyValue key:@"地区" value:@"不限"],
                         [DOKeyValue key:@"行业" value:@"不限"],
                         [DOKeyValue key:@"项目阶段" value:@"不限"]
                         ]
                       ];
    }
    self.tableVC.dataArray = _dataArray;
    
    if (netEngine.chinaAreaArray.count<1) {
        [netEngine requestChinaAreaWithTarget:nil hudView:nil];
    }
    if (netEngine.projectIndustryArray.count<1) {
        [netEngine requestProjectIndustryWithTarget:nil hudView:nil];
    }
    if (netEngine.projectStateArray.count<1) {
        [netEngine requestProjectStateWithTarget:nil hudView:nil];
    }
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak MSPreferenceSetViewController *weakSelf = self;
    if (indexPath.row == 0) {
        MSAreaViewController *vc = [[MSAreaViewController alloc] init];
        vc.ChoicBlock = ^(NSMutableArray *resultArray){
            [weakSelf refreshTableViewWithArray:resultArray index:0];
        };
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (indexPath.row == 1) {
        MSindustryViewController *vc = [[MSindustryViewController alloc] init];
        vc.ChoicBlock = ^(NSMutableArray *resultArray){
            [weakSelf refreshTableViewWithArray:resultArray index:1];
        };
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (indexPath.row == 2) {
        MSStageViewController *vc = [[MSStageViewController alloc] init];
        vc.ChoicBlock = ^(NSMutableArray *resultArray){
            [weakSelf refreshTableViewWithArray:resultArray index:2];
        };
        [rootNavigation pushViewController:vc animated:YES];
    }
}

- (void)refreshTableViewWithArray:(NSMutableArray*)resultArray index:(NSInteger)index{
    NSString *resultStr = [resultArray componentsJoinedByString:@"、"];
    DOKeyValue *doValue = _dataArray[index];
    doValue.value = resultStr.length?resultStr:@"不限";
    _tableVC.dataArray = _dataArray;
    
    NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (DOKeyValue *doValue in _dataArray) {
        [relutArray addObject:doValue.value];
    }
    [relutArray writeToFile:self.preferenceSetFile atomically:YES];
}

- (void)doneClick{
    [rootNavigation popViewControllerAnimated:YES];
}

@end
